package java6681.lesson02;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Junichiro
 * @description 提取三个字符的单词
 * @date 2021-03-14 23:09
 **/

public class Words6681 extends Application {
    /*输出单词的次数*/
    static final int NUM = 1;

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(getRoot(), 500, 300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("提取三个字符的单词");
        primaryStage.show();
    }
    private Parent getRoot(){
        VBox root = new VBox(20);
        Label lblStudent = new Label("212006681|张舒婷");
        /*边框设置*/
        BorderStroke bs = new BorderStroke(Paint.valueOf("#00C5CD")//边框的颜色
                , BorderStrokeStyle.DOTTED//边框的样式
                , new CornerRadii(10)//边框四个角的圆角
                , new BorderWidths(3));//边框的宽度
        /*设置边框*/
        lblStudent.setBorder(new Border(bs));
        /*设置边距*/
        lblStudent.setPadding(new Insets(10));


        /*水平排列两个控件，用于输入*/
        HBox hText = new HBox(10);
        Label lblText = new Label("输入单词：");
        TextField tfText = new TextField();
        /*文本框内无内容的时候显示的信息*/
        tfText.setPromptText("输入单词");
        hText.getChildren().addAll(lblText, tfText);
        /*两个控件的对齐方式*/
        hText.setAlignment(Pos.CENTER_LEFT);

        Button button = new Button("提取单词");
        /*显示排序结果*/
        Label lblPrint = new Label();

        root.getChildren().addAll(lblStudent, hText, button, lblPrint);
        /*设置根节点的边距*/
        root.setPadding(new Insets(20.0));
        /*根节点中各节点的对齐方式*/
        root.setAlignment(Pos.TOP_CENTER);
        /*用CSS样式设置容器中所有节点的字体大小*/
        root.setStyle("-fx-font-size:20");

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String textText = tfText.getText();
                /*规则:三个字符组成的单词*/
                String regex = "\\b\\w{3}\\b";

                /*规则编译成模式对象*/
                Pattern p = Pattern.compile(regex);
                /*通过模式对象得到匹配对象*/
                Matcher m = p.matcher(textText);
                StringBuilder sb = new StringBuilder();
                /*查找出来的字符保存到sb中依次输出*/
                while (m.find()) {
                    for (int i = 0; i < NUM; i++) {
                        String group = m.group();
                        sb.append(group).append(" ");
                    }
                }
                /*转为String*/
                String s = sb.toString();
                /*name new class,my1*/
                lblPrint.setText(s);
            }
        });
        return root;
    }
}
